from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError


# 型号库
class ModelLibrary(models.Model):
    _name = 'sb.model.library'
    _description = 'Model Library'
    _order = "coding_provider_id,code_num"

    coding_provider_id = fields.Many2one('res.partner', string="Supplier", domain="[('supplier_rank', '>', 0)]")
    name = fields.Char(string="Model number")
    category_id = fields.Many2one('product.category', 'Category', domain="[('parent_id.code', 'in', ['CR'])]")
    f_category_id = fields.Many2one('product.category', related="category_id.parent_id")
    code = fields.Char(string='Model code')
    code_num = fields.Integer(string='Model code', compute="compute_code_num", store=True, default=0)
    halogen = fields.Selection([
        ('L', 'halogen'),
        ('H', 'halogen-free')
    ], string="is Halogen")
    core_color = fields.Selection([
        ('Y', '黄-Yellow'),
        ('W', '白-White'),
        ('B', '黑-Black'),
        ('G', '灰-Gray'),
        ('R', '棕-Brown'),
        ('O', '橘-Orange'),
        ('C', '铜色-CuColor'),
        ('L', '透明-Clear'),
        ('N', 'NA')
    ], string="Core color")
    type = fields.Many2one("product.category", string="Type")

    # pp
    pp_name = fields.Char(string="PP Model number")
    pp_category_id = fields.Many2one('product.category', 'PP Category', domain="[('parent_id.code', 'in', ['BS'])]")

    @api.depends('name', 'pp_name')
    def _compute_display_name(self):
        for rec in self:
            if rec.name and rec.pp_name and rec.name != rec.pp_name:
                rec.display_name = rec.name + '/' + rec.pp_name
            elif rec.name:
                rec.display_name = rec.name
            elif rec.pp_name:
                rec.display_name = rec.pp_name
            else:
                rec.display_name = ''

    _sql_constraints = [
        ('name', 'unique (name)', 'Model number sole')
    ]

    @api.depends('code')
    def compute_code_num(self):
        for res in self:
            res.code_num = int(res.code)


# 芯板结构允许尺寸库
class GlassClothTypeList(models.Model):
    _name = 'sb.glass.cloth.type.list'
    _description = 'Glass Cloth Type List'

    name = fields.Char(string="GlassType")
    glassthk = fields.Char(string="GlassThk(mm)")



